import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/channelHelper.dart';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';

import 'package:flutter_downloader/flutter_downloader.dart';
import '../../helper/Helper.dart';
import '../../helper/iconHelper.dart';

class DownLoadBookPage extends StatefulWidget {
  final bool ifAppbar;

  @override
  DownLoadBookPage({this.ifAppbar = false});

  @override
  DownLoadBookPageState createState() => new DownLoadBookPageState();
}

class DownLoadBookPageState extends State<DownLoadBookPage> {
  late List<DownloadTask> DownLoadList;

  void getList() async {
    final tasks = await FlutterDownloader.loadTasks();
    var tasks2 = <DownloadTask>[];

    for (DownloadTask xx in tasks!.reversed) {
      tasks2.add(xx);
    }

    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.

      DownLoadList = tasks2;
    });
  }

  @override
  Widget build(BuildContext context) {
    getList();

    if (DownLoadList == null) return new Container();
    // TODO: implement build
    return Scaffold(
        appBar: widget.ifAppbar ? AppBar(title: Text('下载列表')) : null,
        body: Container(
            margin: EdgeInsets.only(top: 8.0),
            child: ListView.builder(
              itemCount: DownLoadList.length,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
                  shape: const RoundedRectangleBorder(
                      borderRadius: BorderRadius.zero),
                  child: ListTile(
                    //leading: ImageWidget.DownLoadImage(DownLoadList[index].Name),
                    title: Text(DownLoadList[index]!.filename == null
                        ? 'DownLoadList[index]!.filename'
                        : DownLoadList[index]!.filename!),
                    //subtitle: Text(DownLoadList[index].ti),
                    leading: IconHelper.getFileIcon(
                        DownLoadList[index]!.filename!, 24.0),
                    trailing: Container(
                      width: 10.0,
                      height: 10.0,
                      child: new CircleAvatar(
                        backgroundColor: DownLoadList[index].status ==
                                DownloadTaskStatus.complete
                            ? Colors.green
                            : Colors.red,
                        //radius: 2.0,
                      ),
                    ),
                    onTap: () async {
                      //  OpenFile.open(DownLoadList[index].);
                      if (DownLoadList[index].status ==
                          DownloadTaskStatus.complete) {
                        OpenFile.open(DownLoadList[index].savedDir +
                            "//" +
                            DownLoadList[index].filename!);

                        //  FlutterDownloader.open(taskId: DownLoadList[index].taskId);
                        // var f1=  await ChannelHelper.saveFile(DownLoadList[index].savedDir+"//"+DownLoadList[index].filename);
                        //  OpenFile.open(f1);
                      }
                      //FlutterDownloader.open(taskId: DownLoadList[index].taskId);
                    },
                    onLongPress: () async {
                      //  OpenFile.open(DownLoadList[index].);
                      if (DownLoadList[index].status ==
                          DownloadTaskStatus.complete) {
                        //  OpenFile.open(DownLoadList[index].savedDir+"//"+DownLoadList[index].filename);
                        // FlutterDownloader.open(taskId: DownLoadList[index].taskId);
                        var xx = await ChannelHelper.saveFile(
                            DownLoadList[index].savedDir +
                                "//" +
                                DownLoadList[index].filename!);
                        ChannelHelper.showToast("已保存到手机");
                      }
                      //FlutterDownloader.open(taskId: DownLoadList[index].taskId);
                    },
                  ),
                );
              },
            )));
  }
}
